package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.ResponseBean;
import com.pojo.Log;
import com.service.LogService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author: T-bao
 * @Date: 2021-4-8 9:22
 * @Description:
 */
@RestController
@RequestMapping("/log")
public class LogAction {

    @Autowired
    private LogService logService;

    @RequestMapping("/delLog")
    public ResponseBean delLog(@RequestParam(defaultValue = "7") int tian){
        Date logTime = addDate(-tian);
        QueryWrapper<Log> query = new QueryWrapper<Log>().le("caozuoTime",logTime);
        if(logService.list(query).size()>0){
            if(logService.remove(query)){
                return new ResponseBean(200,1,"删除成功","");
            }
            return new ResponseBean(500,1,"删除失败","");
        }
        return new ResponseBean(300,1,"没有可删除数据","");
    }
    //num为需要增加的天数
    public static Date addDate (int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, num);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 根据时间区间或者用户名查询日志
     * @param name          用户名
     * @param page          页数
     * @param limit         每页条数
     * @param start_time    开始时间
     * @param end_time      结束时间
     * @return      mybatisplus 封装
     */
    @RequestMapping("/selectLogPage")
    public ResponseBean selectLogPage(String name, int page, int limit, String start_time, String end_time){

        Page p = new Page(page,limit);
        QueryWrapper query = new QueryWrapper();
        if(StringUtil.isNotNull(start_time)){
            query.ge("caozuoTime",start_time);
        }
        if(StringUtil.isNotNull(end_time)){
            query.le("caozuoTime",end_time);
        }
        if(StringUtil.isNotNull(name)){
            query.like("name",name);
        }
        query.orderByDesc("caozuoTime");
        IPage<Log> iPage = logService.page(p,query);

        return new ResponseBean(0,iPage.getTotal(),"查找成功",iPage.getRecords());
    }

    /**
     * 根据时间区间或者用户名查询日志
     * @param name          用户名
     * @param page          页数
     * @param limit         每页条数
     * @param start_time    开始时间
     * @param end_time      结束时间
     * @return
     */
    @RequestMapping("/selectLog")
    public ResponseBean selectLog(String name, int page, int limit, String start_time, String end_time){

//        System.out.println("操作用户："+name+" 页数："+page+" 每页条数："+limit+" 开始时间："+start_time+" 结束时间："+end_time);
        if(page==1){
            page=0;
        }else{
            page=(page*limit)-limit;
        }
        if(StringUtil.isNull(start_time)){
            start_time=null;
        }
        if(StringUtil.isNull(end_time)){
            end_time=null;
        }
        if(StringUtil.isNull(name)){
            name=null;
        }
        return new ResponseBean(0,logService.selectLogCount(name,start_time,end_time),"成功", logService.selectLog(name,page,limit,start_time,end_time));
    }
}
